(ns jumpluff.core
  (:require [cljs.core.async :as async]
            [jumpluff.state :as state]
            [jumpluff.commands :refer [load-config-then-datasets]]
            [jumpluff.client :refer [client-start client-finish]])
  (:require-macros [cljs.core.async.macros :as async-macros]))

(defn -main []
  (async-macros/go
    (async/<! (load-config-then-datasets))
    (->> js/process.env.DISCORD_TOKEN
       client-start
       (reset! state/client))))

;; Give it 5 seconds to clean up
(let [exit-handler (fn []
                     (js/setTimeout #(.exit js/process 1) 5000)
                     (async-macros/go
                       (async/<! (client-finish @state/client))
                       (.exit js/process 0)))]
  (.on js/process "SIGTERM" exit-handler)
  (.on js/process "SIGINT" exit-handler))
